package equipments

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/equipment_kind"
	"github.com/flipped-aurora/gin-vue-admin/server/model/equipments"
	equipmentsReq "github.com/flipped-aurora/gin-vue-admin/server/model/equipments/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type EquipmentApi struct {
}

var equipmentService = service.ServiceGroupApp.EquipmentsServiceGroup.EquipmentService

// CreateEquipment 创建equipment表
// @Tags Equipment
// @Summary 创建equipment表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body equipments.Equipment true "创建equipment表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /equipment/createEquipment [post]
func (equipmentApi *EquipmentApi) CreateEquipment(c *gin.Context) {
	var equipment equipments.Equipment
	err := c.ShouldBindJSON(&equipment)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := equipmentService.CreateEquipment(&equipment); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		var kind equipment_kind.EquipmentKind
		err := global.GVA_DB.Where("kind_name=?", equipment.EquipmentKind).First(&kind).Error
		if err != nil {
			fmt.Println("没获取到该种类的数量")
		}
		//i := 1
		global.GVA_DB.Update("kind_count=?", +1)

		response.OkWithMessage("创建成功", c)
	}
}

// DeleteEquipment 删除equipment表
// @Tags Equipment
// @Summary 删除equipment表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body equipments.Equipment true "删除equipment表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /equipment/deleteEquipment [delete]
func (equipmentApi *EquipmentApi) DeleteEquipment(c *gin.Context) {
	var equipment equipments.Equipment
	err := c.ShouldBindJSON(&equipment)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := equipmentService.DeleteEquipment(equipment); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteEquipmentByIds 批量删除equipment表
// @Tags Equipment
// @Summary 批量删除equipment表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除equipment表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /equipment/deleteEquipmentByIds [delete]
func (equipmentApi *EquipmentApi) DeleteEquipmentByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := equipmentService.DeleteEquipmentByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateEquipment 更新equipment表
// @Tags Equipment
// @Summary 更新equipment表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body equipments.Equipment true "更新equipment表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /equipment/updateEquipment [put]
func (equipmentApi *EquipmentApi) UpdateEquipment(c *gin.Context) {
	var equipment equipments.Equipment
	err := c.ShouldBindJSON(&equipment)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := equipmentService.UpdateEquipment(equipment); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindEquipment 用id查询equipment表
// @Tags Equipment
// @Summary 用id查询equipment表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query equipments.Equipment true "用id查询equipment表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /equipment/findEquipment [get]
func (equipmentApi *EquipmentApi) FindEquipment(c *gin.Context) {

	var equipment equipments.Equipment

	err := c.ShouldBindQuery(&equipment)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if reequipment, err := equipmentService.GetEquipment(equipment.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reequipment": reequipment}, c)
	}
}

// GetEquipmentList 分页获取equipment表列表
// @Tags Equipment
// @Summary 分页获取equipment表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query equipmentsReq.EquipmentSearch true "分页获取equipment表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /equipment/getEquipmentList [get]
func (equipmentApi *EquipmentApi) GetEquipmentList(c *gin.Context) {
	var pageInfo equipmentsReq.EquipmentSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = utils.Verify(pageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	list, total, err := equipmentService.GetEquipmentInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// @Router /equipment/getEquipmentNumber [get]
func (equipmentApi *EquipmentApi) GetEquipmentNumber(c *gin.Context) {

	number, err := equipmentService.GetEquipmentNumber()

	if err != nil {
		fmt.Println("查看number", err.Error())
		return
	}

	response.OkWithDetailed(number, "获取成功", c)
}

// @Router /equipment/getEquipmentDevice [get]
func (equipmentApi *EquipmentApi) GetEquipmentDevice(c *gin.Context) {

	info, err := equipmentService.GetEquipmentDevice()
	if err != nil {
		fmt.Println("查看number", err.Error())
		return
	}

	response.OkWithDetailed(info, "获取成功", c)
}

// @Router /equipment/getEquipmentBadList [get]
func (equipmentApi *EquipmentApi) GetEquipmentBad(c *gin.Context) {
	var pageInfo request.GetDataSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = utils.Verify(pageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	info, err := equipmentService.GetEquipmentBad(pageInfo)
	if err != nil {
		fmt.Println("查看number", err.Error())
		return
	}

	response.OkWithDetailed(info, "获取成功", c)
}

// @Router /equipment/getEquipmentUseList [get]
func (equipmentApi *EquipmentApi) GetEquipmentUse(c *gin.Context) {
	var pageInfo request.GetDataSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = utils.Verify(pageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	info, err := equipmentService.GetEquipmentUse(pageInfo)
	if err != nil {
		fmt.Println("查看number", err.Error())
		return
	}

	response.OkWithDetailed(info, "获取成功", c)
}

// @Router /equipment/getEquipmentRestList [get]
func (equipmentApi *EquipmentApi) GetEquipmentRest(c *gin.Context) {
	var pageInfo request.GetDataSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = utils.Verify(pageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	info, err := equipmentService.GetEquipmentRest(pageInfo)
	if err != nil {
		fmt.Println("查看number", err.Error())
		return
	}

	response.OkWithDetailed(info, "获取成功", c)
}
